import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class StudentManagementPanel extends JFrame {
    private ArrayList<Student> students;
    private DefaultListModel<String> listModel = new DefaultListModel<>();

    public StudentManagementPanel(ArrayList<Student> students) {
        this.students = students;
        setTitle("学生管理面板");
        setSize(500, 400);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel panel = new JPanel();
        add(panel);
        placeComponents(panel);

        // 初始化列表
        initializeStudentList();
    }

    private void placeComponents(JPanel panel) {
        panel.setLayout(null);

        JLabel studentListLabel = new JLabel("学生列表");
        studentListLabel.setBounds(10, 20, 80, 25);
        panel.add(studentListLabel);

        JList<String> studentList = new JList<>(listModel);
        JScrollPane scrollPane = new JScrollPane(studentList);
        scrollPane.setBounds(10, 50, 160, 200);
        panel.add(scrollPane);

        JButton addButton = new JButton("添加学生");
        addButton.setBounds(180, 50, 120, 25);
        panel.add(addButton);

        JButton editButton = new JButton("编辑学生");
        editButton.setBounds(180, 90, 120, 25);
        panel.add(editButton);

        JButton deleteButton = new JButton("删除学生");
        deleteButton.setBounds(180, 130, 120, 25);
        panel.add(deleteButton);

        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                StudentDialog studentDialog = new StudentDialog(StudentManagementPanel.this, "添加学生", null);
                studentDialog.setVisible(true);
            }
        });

        editButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int selectedIndex = studentList.getSelectedIndex();
                if (selectedIndex != -1) {
                    Student selectedStudent = students.get(selectedIndex);
                    StudentDialog studentDialog = new StudentDialog(StudentManagementPanel.this, "编辑学生", selectedStudent);
                    studentDialog.setVisible(true);
                } else {
                    JOptionPane.showMessageDialog(null, "请选择一个学生进行编辑");
                }
            }
        });

        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int selectedIndex = studentList.getSelectedIndex();
                if (selectedIndex != -1) {
                    students.remove(selectedIndex);
                    listModel.remove(selectedIndex);
                } else {
                    JOptionPane.showMessageDialog(null, "请选择一个学生进行删除");
                }
            }
        });
    }

    private void initializeStudentList() {
        for (Student student : students) {
            listModel.addElement(student.getName());
        }
    }

    public void addStudent(Student student) {
        students.add(student);
        listModel.addElement(student.getName());
    }

    public void updateStudent(Student student) {
        int index = students.indexOf(student);
        if (index != -1) {
            students.set(index, student);
            listModel.set(index, student.getName());
        }
    }
}
